package com.atguigu.yygh.hosp.controller.api;


import com.atguigu.yygh.hosp.bean.Result;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.utils.HttpRequestHelper;
import com.atguigu.yygh.hosp.utils.MD5;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.result.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/*==========================================
      时 间:   2022-04-18
      讲 师:   刘  辉
      描 述:   尚硅谷教学团队
 ==========================================*/
@RestController
@RequestMapping("/api/hosp")
public class ApiHospitalController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;


    //查询医院信息
    @PostMapping("/hospital/show")
    public Result<Hospital> getHosptialInfo(HttpServletRequest request){
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String)stringObjectMap.get("hoscode");
        //todo: sign校验
        Hospital hospital=hospitalService.getHosptialInfo(hoscode);
        return Result.ok(hospital);
    }


   //保存医院信息
    @PostMapping("/saveHospital")
    public Result saveHospital(HttpServletRequest request){
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(request.getParameterMap());

        String sign = (String)stringObjectMap.get("sign");
        QueryWrapper<HospitalSet> hospitalSetQueryWrapper=new QueryWrapper<HospitalSet>();
        Object hoscode = stringObjectMap.get("hoscode");
        hospitalSetQueryWrapper.eq("hoscode",hoscode);
        HospitalSet hospitalSet = hospitalSetService.getOne(hospitalSetQueryWrapper);
        String signKey = hospitalSet.getSignKey();

        String encrypt = MD5.encrypt(signKey);
        if(!StringUtils.isEmpty(encrypt) && !StringUtils.isEmpty(sign) && sign.equals(encrypt)){
            String logoData = (String)stringObjectMap.get("logoData");
            logoData = logoData.replaceAll(" ", "+");
            stringObjectMap.put("logoData",logoData);
            hospitalService.save(stringObjectMap);
        }

        return Result.ok();
    }


}
